/**
 * 登录注册入口文件
 */
define(function (require, exports, module) {


    var $welcome = $(".welcome"), $form = $welcome.find("form")


    var reg_password = /^[a-zA-Z0-9]{6,16}$/;
    var password_rule = "密码为6-12个数字或字母组合";



    //<editor-folder desc='协议选择'>
    var $span = $form.find(".txt span");
    $span.unbind("click.selected").bind("click.selected", function () {
        var $this = $(this), $ipt = $this.siblings("input[type='hidden']");
        if ($this.hasClass("selected")) {
            $ipt.val(false)
            $this.removeClass("selected")
        } else {
            $ipt.val(true)
            $this.addClass("selected");
        }
    })
    //</editor-folder>



    var toolTip = require("ui/tip")
    var $GIpt = $form.find(".G_ipt").each(function () {
        var $this = $(this), _placeholder = $this.attr("placeholder");
        var $par = $this.parent(), dir = "right", _msg = "请输入用户名";

        if ($par.hasClass("verify") || $par.hasClass("note")) {
            dir = "left"
        }

        if ($par.hasClass("password")) {
            _msg = "请输入密码"
        } else if ($par.hasClass("verify")) {
            _msg = "请输入验证码"
        }

        var tipInst = new toolTip($this, _placeholder, dir)
        $this.unbind("focus.ipt").bind("focus.ipt", function (event, msg) {
            $this.attr("placeholder", "")
            var _tipMsg = msg ? msg : _placeholder
            tipInst.show(_tipMsg, false);

        }).unbind("blur.ipt").bind("blur.ipt", function () {
            $this.attr("placeholder", _placeholder);
            tipInst.remove();
        })
    })
    $form.find("[type='submit']").unbind("click.sub").bind("click.sub", function () {
        var validate_success=true;
        $GIpt.each(function () {
            var $this = $(this), _val = $this.val(),$par = $this.parent();
            if (!_val) {
                var _msg = "请输入用户名"
                if ($par.hasClass("password")) {
                    _msg = "请输入密码"
                } else if ($par.hasClass("verify")) {
                    _msg = "请输入验证码"
                }
                $this.trigger("focus.ipt", [_msg])
                return false
            }else{
                if ($par.hasClass("password")) {
                    if (!_val.match(reg_password)) {
                        $this.trigger("focus.ipt", [password_rule])
                    }
                }
            }
        })

        return false
    })
})